package com.junhao.springes.controller;

import com.junhao.springes.dao.UserRepository;
import com.junhao.springes.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author Lee
 * @Date 2021/12/20
 * @Description
 */


/**
 * es
 */

@RestController
public class EsController {


    @Autowired
    private UserRepository userRepository;

    /**
     * 数据添加进es
     */
    @RequestMapping("/addUser2Es")
    public void addUser2Es(){
        User user= new User();
        user.setId(1L);
        user.setAddress("北京");
        user.setSex("男");
        user.setUserName("mike10");
        userRepository.index(user);
    }

    @RequestMapping("/addUser")
    public void addUser(){
        for (int i = 10; i < 20; i++) {

            User user= new User();
            user.setId((long)i);
            user.setAddress("重庆");
            user.setSex("男");
            user.setUserName("mike"+i);
            userRepository.index(user);
        }
    }

    /**
     * 从es查询数据
     */
    @RequestMapping("/query")
    public String query(){
        List<User> userList = userRepository.findByUserNameLike("mike");
        return userList.toString();

    }

    @GetMapping("/all")
    public Iterable<User> all(){

        System.out.println(userRepository.findAll().spliterator());
        return userRepository.findAll();

    }

    @GetMapping("/delUser")
    public void delUser(){
        userRepository.deleteById(1L);



        userRepository.save(new User());


    }

    @GetMapping("/byAddress")
    public List byAddress(){
        return userRepository.findAllByAddress("重庆");
    }


//




}
